package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class AuanyUserloginFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	protected DecimalFormat df = new DecimalFormat("######0.00");
	protected Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime()));
	protected Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime()));

	private AuanyUserloginFlow queryBean; // 查询bean

	private List<Object[]> result; // 结果List

	@Autowired
	HadoopQueryManager hadoopQueryManager;

	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;

	@PostConstruct
	public void init() {
		queryBean = new AuanyUserloginFlow();
		result = new ArrayList<>();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "流水查询—新用户登录流水");

		result = new ArrayList<>();

		FacesContext context = FacesContext.getCurrentInstance();

		// 开始结束时间不能大于当前时间60天
		if (Time.getInterval(new Date(), dateStart) > 60 || Time.getInterval(new Date(), dateEnd) > 60) {
			context.addMessage(null, new FacesMessage("提示", "开始结束时间不能小于当前时间60天!"));
			return;
		}

		// 全用户名，角色ID必须至少有一个非空!
		if (StringUtils.isBlank(queryBean.getFulluseridentity()) && StringUtils.isBlank(queryBean.getUserid())) {
			context.addMessage(null, new FacesMessage("提示", "全用户名，角色ID必须至少有一个非空!"));
			return;
		}

		if (HadoopQueryManager.VISIT_QUEUE.offer("AuanyUserloginFlowAction")) {
			System.out.println("VISIT_QUEUE offer AuanyUserloginFlowAction ok!");

			StringBuffer sql = new StringBuffer();
			sql.append("select logtime,hostname,modulename,gameid,auanyserverid,logid,logtag,appid,devicecategory,fulluseridentity,userid,zonid from auany_userlogin where 1=1 ");
			if (queryBean.getUserid() != null && !"".equals(queryBean.getUserid())) {
				sql.append(" and userid = '");
				sql.append(queryBean.getUserid());
				sql.append("' ");
			}
			if (!"0".equals(queryServer)) {
				sql.append(" and zoneid = '");
				sql.append(queryServer);
				sql.append("' ");
			}
			if (queryBean.getAuanyserverid() != null && !"".equals(queryBean.getAuanyserverid())) {
				sql.append(" and auanyserverid = '");
				sql.append(queryBean.getAuanyserverid().trim());
				sql.append("' ");
			}
			if (queryBean.getFulluseridentity() != null && !"".equals(queryBean.getFulluseridentity())) {
				sql.append(" and fulluseridentity = '");
				sql.append(queryBean.getFulluseridentity().trim());
				sql.append("' ");
			}
			sql.append(" and dt >= '");
			sql.append(Time.getDateStr(dateStart));
			sql.append("' and dt <= '");
			sql.append(Time.getDateStr(dateEnd));
			sql.append("' ");
			sql.append(" order by logtime desc ");

			Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
			if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue()) {
				context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
				return;
			}

			result = ret.getValue();
		} else {
			System.out.println("VISIT_QUEUE offer AuanyUserloginFlowAction failed!");
			context.addMessage(null, new FacesMessage("提示", "当前系统正被访问，为保证服务器稳定，请稍后再试"));
			return;
		}
	}

	public class AuanyUserloginFlow {
		private String dt; // 日期
		private String logtime; // 时间
		private String hostname; // 服务器主机名
		private String modulename; // 模块名
		private String gameid;// 游戏id
		private String auanyserverid; // au服务器编号
		private String logid; // 日志id
		private String logtag; // 日志类型
		private String appid; // 渠道游戏id
		private String devicecategory;// 设备类型
		private String fulluseridentity;// 全用户名
		private String userid;// auany用户表userid
		private String zonid;// 区服Id

		public String getDt() {
			return dt;
		}

		public void setDt(String dt) {
			this.dt = dt;
		}

		public String getLogtime() {
			return logtime;
		}

		public void setLogtime(String logtime) {
			this.logtime = logtime;
		}

		public String getHostname() {
			return hostname;
		}

		public void setHostname(String hostname) {
			this.hostname = hostname;
		}

		public String getModulename() {
			return modulename;
		}

		public void setModulename(String modulename) {
			this.modulename = modulename;
		}

		public String getGameid() {
			return gameid;
		}

		public void setGameid(String gameid) {
			this.gameid = gameid;
		}

		public String getAuanyserverid() {
			return auanyserverid;
		}

		public void setAuanyserverid(String auanyserverid) {
			this.auanyserverid = auanyserverid;
		}

		public String getLogid() {
			return logid;
		}

		public void setLogid(String logid) {
			this.logid = logid;
		}

		public String getLogtag() {
			return logtag;
		}

		public void setLogtag(String logtag) {
			this.logtag = logtag;
		}

		public String getAppid() {
			return appid;
		}

		public void setAppid(String appid) {
			this.appid = appid;
		}

		public String getDevicecategory() {
			return devicecategory;
		}

		public void setDevicecategory(String devicecategory) {
			this.devicecategory = devicecategory;
		}

		public String getFulluseridentity() {
			return fulluseridentity;
		}

		public void setFulluseridentity(String fulluseridentity) {
			this.fulluseridentity = fulluseridentity;
		}

		public String getUserid() {
			return userid;
		}

		public void setUserid(String userid) {
			this.userid = userid;
		}

		public String getZonid() {
			return zonid;
		}

		public void setZonid(String zonid) {
			this.zonid = zonid;
		}

	}

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public AuanyUserloginFlow getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(AuanyUserloginFlow queryBean) {
		this.queryBean = queryBean;
	}

	public List<Object[]> getResult() {
		return result;
	}

	public void setResult(List<Object[]> result) {
		this.result = result;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}
}
